﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Datorgrafik_lab2.Helpers;

namespace Datorgrafik_lab2.Objects
{
    public class Water : GameObject
    {
        private int numRow = 100;
        private int numCol = 100;
        private int radius = 100;

        float sealevel = 0;

        Color[] colorData;            

        public Water(GraphicsDevice graphics, Vector3 pos, float scale, Texture2D texture)
            : base(graphics, pos, Quaternion.Identity, scale, texture)
        {           
        }        

        protected override void InitializeModel() {
            this.colorData = new Color[this.texture.Height * this.texture.Width];
            this.texture.GetData<Color>(colorData);

            initializeGround();
        }

        private void initializeGround() {

            initializeVertices();

            initializeIndices();

        }

        public void Update(float waterlevel)
        {
            this.sealevel = waterlevel;

            initializeVertices();
        }


        private void initializeVertices() {
            vertices = new VertexPositionColorTexture[numRow * numCol];

            this.colorData = new Color[this.texture.Height * this.texture.Width];
            this.texture.GetData<Color>(this.colorData);

            double pixelHeightDistance = (double)this.texture.Height / this.numRow;
            double pixelWidthDistance = (double)this.texture.Width / this.numCol;

            int yMax = numRow - 1;
            int xMax = numCol - 1;

            for(int row = 0; row < numRow; row++) {
                for(int col = 0; col < numCol; col++) {

                    int colorIndex =
                        (int)((double)row * pixelHeightDistance) * this.texture.Width
                        + (int)((double)col * pixelWidthDistance);                   

                    float height = sealevel;

                    //Set position to a sphere
                    Vector3 sphere = new Vector3(
                        col,
                        height,
                        row
                        );

                    double ringradius = (float)((height + radius) * Math.Sin(row * Math.PI / yMax));

                    Vector3 xyz = new Vector3(
                        (float)(Math.Cos((xMax - col) * Math.PI * 2.0f / xMax) * ringradius),
                        (float)(Math.Cos(row * Math.PI / yMax) * radius),
                        (float)(Math.Sin((xMax - col) * Math.PI * 2.0f / xMax) * ringradius));  
                    
                    
                    //Set position to a plane
                    Vector3 position = new Vector3(
                        col,                                             
                        height,
                        row
                        );

                    //CSwitch between plane or sphere
                    position = xyz;

                    this.vertices[(row * numCol) + col] = new VertexPositionColorTexture(
                        position, 
                        Color.Blue,
                        new Vector2((float)col / (numCol-1), (float)row / (numRow-1)));
                }
            }
        }

        private void initializeIndices() {
            //6 vertices make up 2 triangles. Not decreasing Col because we want it to wrap around
            indices = new int[(numRow) * (numCol) * 6];

            int counter = 0;
            //Indices
            for(int row = 0; row < numRow-1 ; row++) {
                for(int col = 0; col < numCol - 1; col++) {
                    int topLeft = (row * numCol) + col;
                    int topRight = (row * numCol) + col+1;
                    int bottomLeft = ((row + 1) * numCol) + col;
                    int bottomRight = ((row + 1) * numCol) + col+1;                            

                    indices[counter++] = topLeft;
                    indices[counter++] = topRight;
                    indices[counter++] = bottomLeft;

                    indices[counter++] = topRight;
                    indices[counter++] = bottomRight;
                    indices[counter++] = bottomLeft;                    
                }
            }
        }
    }
}
